package com.hdhere.application.Repository.permission;

import com.hdhere.application.Repository.BaseRepository;
import com.hdhere.domain.*;
import com.querydsl.core.types.Projections;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.querydsl.core.group.GroupBy.groupBy;
import static com.querydsl.core.group.GroupBy.list;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date: 2019-04-27 14:04
 * @Description:
 * @version 1.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0      2019/4/27       dustin                               
 *******************************************************************************************
 */
public class SysRoleRepositoryImpl extends BaseRepository implements SysRoleRepositoryCus {
    @Override
    public List<SysRole> getRoleWithPermission(Long uid) {
        QUserInfo qUser = QUserInfo.userInfo;
        QSysRole qRole = QSysRole.sysRole;
        QSysUserRole qUserRole = QSysUserRole.sysUserRole;
        QSysPermission qPermission = QSysPermission.sysPermission;
        QSysRolePermission qRolePermission = QSysRolePermission.sysRolePermission;

        Map<Integer,SysRole> userRole=
                queryFactory.from(qUser)
                        .join(qUserRole).on(qUser.id.eq(qUserRole.userId))
                        .join(qRole).on(qRole.id.eq(qUserRole.roleId))
                        .join(qRolePermission).on(qRole.id.eq(qRolePermission.roleId))
                        .join(qPermission).on(qPermission.id.eq(qRolePermission.permissionId))
                        .where(qUser.id.eq(uid))
                        .transform(groupBy(qRole.id).as(Projections.bean(SysRole.class,qRole.id,qRole.name,qRole.role,qRole.available,
                                list(Projections.bean(SysPermission.class, qPermission.id,qPermission.name,qPermission.permission)
                                ).as("permissions")
                        )));

        return new ArrayList<>(userRole.values());
    }
}
